import React, {Component} from 'react';
import {View, Text} from 'react-native';
import JMessage from "./src/utils/JMessage";

// 全局数据状态管理
import {Provider} from "mobx-react";
import RootStore from './src/mobx';
import PublishStore from './src/mobx/publist.js';
import UserInfoStore from './src/mobx/userInfo.js';

import Nav from './src/nav.js';
import Geo from "./src/utils/Geo";
import {getUserInfo} from "./src/utils/AsyncStorage";

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      isInitGeo: false,
    }
  }

  async componentDidMount(): void {
    // 高德地图初始化
    await Geo.initGeo();

    // 极光初始化
    JMessage.init();

    // 等待高德地图初始化之后，再渲染页面
    this.setState({
      isInitGeo: true
    });


    let res = await getUserInfo();
    if(res){
      let mobile = res.mobile;
      let token = res.token;
      let userId = res.userId;
      if (mobile && token && userId){
        this.props.RootStore.setUserInfo(mobile, token, userId);
      }
    }
  }

  render() {
    let {isInitGeo} = this.state;
    return (
      <View style={{flex: 1}}>
        <Provider RootStore={RootStore} PublishStore={PublishStore} UserInfoStore={UserInfoStore}>
          {isInitGeo ? <Nav/> : <></>}
        </Provider>
      </View>
    )
  }
}
